/**
 * 
 */
package net.sos.translator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Properties;

/**
 * @author SANJAY
 * 
 */
public class PropertiesFileTranslatorImpl implements ITextFileTranslator {

   final String NEW_LINE = System.getProperty("line.separator");

   protected ITranslator translator;

   /*
    * (non-Javadoc)
    * 
    * @see
    * net.sos.translator.ITextFileTranslator#setTranslator(net.sos.translator
    * .ITranslator)
    */
   @Override
   public void setTranslator(ITranslator translator) {
      this.translator = translator;
   }

   /*
    * (non-Javadoc)
    * 
    * @see net.sos.translator.ITextFileTranslator#translateFile(java.io.File,
    * java.io.File)
    */
   @Override
   public void translateFile(File inFile, File outFile)
         throws TranslationException {
      try {
         BufferedReader in = new BufferedReader(new InputStreamReader(
               new FileInputStream(inFile)));
         PrintWriter out = new PrintWriter(outFile);
         try {
            Properties newProperties = new Properties();
            Properties oldProperties = new Properties();
            oldProperties.load(in);
            for (Map.Entry<Object, Object> entry : oldProperties.entrySet()) {
               newProperties.put(entry.getKey(), translator.translate(entry
                     .getValue().toString()));
            }
            newProperties.store(out, translator.translate("Translated file"));
         } finally {
            if (in != null) {
               in.close();
            }
            if (out != null) {
               out.flush();
               out.close();
            }
         }
      } catch (Exception e) {
         throw new TranslationException(e.getMessage(), e);
      }

   }

}
